package com.lj.luoye.lotteryticket;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.kaopiz.kprogresshud.KProgressHUD;
import com.lj.luoye.lotteryticket.dao.LotteryBuyDao;
import com.lj.luoye.lotteryticket.dao.LotteryBuyMultiDao;
import com.lj.luoye.lotteryticket.dialog.MyDialogUtil;
import com.lj.luoye.lotteryticket.entity.Unionlotto;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class StatisticActivity extends AppCompatActivity {
    private LotteryBuyDao lotteryBuyDao;
    private LotteryBuyMultiDao lotteryBuyMultiDao;

    private Spinner lotteryMonthSp;
    private ArrayAdapter<String> adapter;

    private TextView buyPeriodCountTv;
    private TextView buyCountTv;
    private TextView buyAmountTv;
    private TextView luckCountTv;
    private TextView luckAmountTv;

    private TextView luck1CountTv;
    private TextView luck2CountTv;
    private TextView luck3CountTv;
    private TextView luck4CountTv;
    private TextView luck5CountTv;
    private TextView luck6CountTv;


    private Handler statisticHandler;
    private KProgressHUD hud;
    private void statistic(final String month){
        hud.show();
        new Thread(new Runnable() {
            @Override
            public void run() {
                //统计
                List<Unionlotto> unionlottoList = lotteryBuyDao.getBoughtByMonth(month);

                int bpc=0;
                int bc=0;
                int ba=0;
                int lc=0;
                int la=0;
                int l1c=0;
                int l2c=0;
                int l3c=0;
                int l4c=0;
                int l5c=0;
                int l6c=0;
                boolean bigLuck=false;
                String d="";
                //单选统计
                for(Unionlotto unionlotto:unionlottoList){

//                    System.out.print(unionlotto.getDate()+":");
//                    for(String redBall:unionlotto.getRedBalls()){
//                        System.out.print(redBall+" ");
//                    }
//                    System.out.println("- "+unionlotto.getBlueBall());

                    if(!d.equals(unionlotto.getDate())){
                        bpc++;
                        d=unionlotto.getDate();
                    }
                    bc++;
                    ba+=2;//每注两元
                    if(unionlotto.getLuckResult()!=null&&unionlotto.getLuckResult().contains("等奖")){//中奖的
                        lc++;
                        switch (unionlotto.getLuckResult()){
                            case "六等奖":
                                la+=5;
                                l6c++;
                                break;
                            case "五等奖":
                                la+=10;
                                l5c++;
                                break;
                            case "四等奖":
                                la+=200;
                                l4c++;
                                break;
                            case "三等奖":
                                la+=3000;
                                l3c++;
                                break;
                            case "二等奖":
                                bigLuck=true;
                                l2c++;
                                break;
                            case "一等奖":
                                bigLuck=true;
                                l1c++;
                                break;
                        }
                    }
                }
                //复选统计
                List<Unionlotto> unionlottoListMulti = lotteryBuyMultiDao.getBoughtByMonth(month);
                for(Unionlotto unionlotto:unionlottoListMulti){
                    int size = unionlotto.expand().size();
                    bc+=size;//注数
                    ba+=2*size;//每注两元，购彩金额
                    if(unionlotto.getLuckResult()!=null&&unionlotto.getLuckResult().contains("等奖")){//中奖的
                        String[] sss = unionlotto.getLuckResult().split(" ");
                        for(String ss:sss){
                            if(!"".equals(ss)){
                                String[] split = ss.split("\\*");
                                if(split.length==2){
                                    String lll=split[0];
                                    String countStr=split[1];
                                    int count=0;
                                    try {
                                        count = Integer.parseInt(countStr);
                                    }catch (Exception e){}
                                    lc+=count;
                                    switch (lll){
                                        case "六等奖":
                                            la+=5*count;
                                            l6c+=count;
                                            break;
                                        case "五等奖":
                                            la+=10*count;
                                            l5c+=count;
                                            break;
                                        case "四等奖":
                                            la+=200*count;
                                            l4c+=count;
                                            break;
                                        case "三等奖":
                                            la+=3000*count;
                                            l3c+=count;
                                            break;
                                        case "二等奖":
                                            bigLuck=true;
                                            l2c+=count;
                                            break;
                                        case "一等奖":
                                            bigLuck=true;
                                            l1c+=count;
                                            break;
                                    }
                                }
                            }
                        }
                    }



                }

                //发送统计结果给主线程
                Message msg=new Message();
                Bundle bundle=new Bundle();
                bundle.putBoolean("ok",true);
                String str=bpc+"";
                bundle.putString("bpct",str);
                str=bc+"";
                bundle.putString("bct",str);
                str="￥"+ba+".00";
                bundle.putString("bat",str);
                str=""+lc;
                bundle.putString("lct",str);
                if(bigLuck) {
                    str = "<font color='red'>中了大奖，无法计算金额</font> ";
                }else{
                    str = "￥" + la + ".00";
                }
                bundle.putString("lat",str);

                str=""+l1c;
                bundle.putString("l1ct",str);
                str=""+l2c;
                bundle.putString("l2ct",str);
                str=""+l3c;
                bundle.putString("l3ct",str);
                str=""+l4c;
                bundle.putString("l4ct",str);
                str=""+l5c;
                bundle.putString("l5ct",str);
                str=""+l6c;
                bundle.putString("l6ct",str);
                msg.setData(bundle);
                statisticHandler.sendMessage(msg);

                hud.dismiss();
            }
        }).start();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_statistic);

        buyPeriodCountTv=findViewById(R.id.buyPeriodCountTv);
        buyCountTv=findViewById(R.id.buyCountTv);
        buyAmountTv=findViewById(R.id.buyAmountTv);
        luckCountTv=findViewById(R.id.luckCountTv);
        luckAmountTv=findViewById(R.id.luckAmountTv);
        luck1CountTv=findViewById(R.id.luck1CountTv);
        luck2CountTv=findViewById(R.id.luck2CountTv);
        luck3CountTv=findViewById(R.id.luck3CountTv);
        luck4CountTv=findViewById(R.id.luck4CountTv);
        luck5CountTv=findViewById(R.id.luck5CountTv);
        luck6CountTv=findViewById(R.id.luck6CountTv);

        lotteryBuyDao=new LotteryBuyDao(getApplicationContext());
        lotteryBuyMultiDao=new LotteryBuyMultiDao(getApplicationContext());
        lotteryMonthSp=findViewById(R.id.lotteryMonthSp);

        final List<String> months=lotteryBuyDao.getMonths();
        List<String> months1 = lotteryBuyMultiDao.getMonths();
        for(String s:months1){
            boolean in=false;
            for(String m:months){
                if(m.equals(s)){
                    in=true;
                    break;
                }
            }
            if(!in){
                months.add(s);
            }
        }
        Collections.sort(months, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o1.compareTo(o2)*(-1);
            }
        });
        months.add(0,"开始以来");

        //将可选内容与ArrayAdapter连接起来
        adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,months);
        //设置下拉列表的风格
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        //将adapter 添加到spinner中
        lotteryMonthSp.setAdapter(adapter);
        //添加事件Spinner事件监听
        lotteryMonthSp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String month="";
                if(position==0){//统计全部

                }else {//按月统计
                    month = months.get(position);
                }
                statistic(month);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        //设置默认值
        lotteryMonthSp.setVisibility(View.VISIBLE);

        hud=KProgressHUD.create(this);
        hud.setCancellable(false);
        statisticHandler=new Handler(){
            @Override
            public void handleMessage(@NonNull Message msg) {
                boolean ok=msg.getData().getBoolean("ok");
                if(ok) {
                    String str="";
                    str = msg.getData().getString("bpct");
                    buyPeriodCountTv.setText(Html.fromHtml(str));
                    str = msg.getData().getString("bct");
                    buyCountTv.setText(Html.fromHtml(str));
                    str = msg.getData().getString("bat");
                    buyAmountTv.setText(Html.fromHtml(str));
                    str = msg.getData().getString("lct");
                    luckCountTv.setText(Html.fromHtml(str));
                    str = msg.getData().getString("lat");
                    luckAmountTv.setText(Html.fromHtml(str));

                    str = msg.getData().getString("l1ct");
                    luck1CountTv.setText(Html.fromHtml(str));
                    str = msg.getData().getString("l2ct");
                    luck2CountTv.setText(Html.fromHtml(str));
                    str = msg.getData().getString("l3ct");
                    luck3CountTv.setText(Html.fromHtml(str));
                    str = msg.getData().getString("l4ct");
                    luck4CountTv.setText(Html.fromHtml(str));
                    str = msg.getData().getString("l5ct");
                    luck5CountTv.setText(Html.fromHtml(str));
                    str = msg.getData().getString("l6ct");
                    luck6CountTv.setText(Html.fromHtml(str));
                }else {
                    String m=msg.getData().getString("msg");
                    MyDialogUtil.showMessageDialog(StatisticActivity.this,"注意",m);
                }
            }
        };
    }


}
